package org.noote.libs.spidy.comm;

import java.util.Locale;

import org.noote.libs.tcpip.IPEvent_Server;
import org.noote.libs.tcpip.IPServer_ConnectedThread;

import android.util.Log;

//////////////////////////////////
///
/// 
///
/////////////////////////////////

public class IPServerCommandEvents implements IPEvent_Server,IPServerCommandEvents_Interface {
	static final String TAG = "IPServerCommandHandler";
	private boolean _isDebug = false;
	IPServer_ConnectedThread _thread;

	@Override
	public void setThread(IPServer_ConnectedThread thread)
	{
		this._thread = thread;  
	}
	
	protected void sendMessage(String sMsg) {
		_thread.sendMessage(sMsg);
	}

	@Override
	public void onConnected() {
	}
	@Override
	public void onDisconnected() {
	}
	@Override
	public void onError(String sError) {
	}
	@Override
	public void onText(String sText) {
	}
	@Override
	public void onCmd(String sMessage)
	{
    	if(_isDebug)
    		Log.d(TAG, "Get cmd : "+sMessage);
		String [] sCmds = sMessage.split("\\:");
		if(sCmds.length==2)
		{
			String sCmd = sCmds[0].trim().toLowerCase(Locale.getDefault());
			String sOps = sCmds[1].trim().toLowerCase(Locale.getDefault());
        	if(sCmd.equals("error"))
        	{
        		if(sOps.length()>0)
        			onError(sOps);
        	}
        	else if(sCmd.equals("message"))
        	{
        		if(sOps.length()>0)
        			onText(sOps);
        	}
        	else if(sCmd.equals("quit"))
        	{
        		onDisconnected();
        	}
        	else if(sCmd.equals("version"))
        	{
    			onAskVersion();
        	}
        	else if(sCmd.equals("version_hello"))
        	{
        		String sVersion="", sName="";
        		
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			String [] sValues = sParam.split("\\=");
        			if(sValues[0].equals("version"))
        				sVersion = sValues[1];
        			else if(sValues[0].equals("name"))
        				sName = sValues[1];
        		}
    			onHello(sVersion, sName);
        	}
        	else if(sCmd.equals("setting_ask"))
        	{
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			if(sParam.equals("all"))
        			{
        				onAskSetting();
        			}
        			else if(sParam.equals("camera_bc"))
        			{
        				onAskSettingCameraBroadcast();
        			}
        		}
        	}
        	else if(sCmd.equals("status_ask"))
        	{
        		onAskStatus();
        	}
        	else if(sCmd.equals("reset"))
        	{
        		onReset();
        	}
    	}
    }

	@Override
	public void onHello(String sVersion, String sControllerName) {
	}
	@Override
	public void onAskVersion() {
	}
	@Override
	public void onAskSetting() {
	}
	@Override
	public void onAskSettingCameraBroadcast() {
	}
	@Override
	public void onAskStatus() {
	}
	@Override
	public void onReset() {
	}
	@Override
	public void onAskVibrate(long [] datas)
	{
	}
}
